package com.example.himalaya.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.model.ISubDao;
import com.example.himalaya.model.XimalayaDBHelper;
import com.example.himalaya.view.callback.ISubDaoCallback;
import com.example.himalaya.constants.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;

import java.util.ArrayList;
import java.util.List;

public class SubscriptionDao implements ISubDao {

    // Constant
    private static final String TAG = "SubscriptionDao";


    private static final SubscriptionDao ourInstance = new SubscriptionDao();

    // Object
    private final XimalayaDBHelper mXimalayaDBHelper;

    // Callback
    private ISubDaoCallback mCallback = null;

    private Object mLock = new Object();


    public static SubscriptionDao getInstance() {
        return ourInstance;
    }

    private SubscriptionDao() {
        mXimalayaDBHelper = new XimalayaDBHelper(BaseApplication.getAppContext());
    }

    @Override
    public void setCallback(ISubDaoCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void addAlbum(Album album) {

        synchronized (mLock) {

            SQLiteDatabase db = null;

            boolean isSuccess = false;

            try {
                db = mXimalayaDBHelper.getWritableDatabase();
                db.beginTransaction();

                ContentValues constentValues = new ContentValues();

                // 封装数据
                constentValues.put(Constants.SUB_COVER_URL, album.getCoverUrlLarge());
                constentValues.put(Constants.SUB_TITLE, album.getAlbumTitle());
                constentValues.put(Constants.SUB_DESCRIPTION, album.getAlbumIntro());
                constentValues.put(Constants.SUB_PLAY_COUNT, (int) album.getPlayCount());
                constentValues.put(Constants.SUB_TRACK_COUNT, album.getIncludeTrackCount());
                constentValues.put(Constants.SUB_AUTHOR_NAME, album.getAnnouncer().getNickname());
                constentValues.put(Constants.SUB_ALBUM_ID, album.getId());

                db.insert(Constants.SUB_TB_NAME, null, constentValues);

                db.setTransactionSuccessful();

                isSuccess = true;

            } catch (Exception e) {
                e.printStackTrace();
                isSuccess = false;

            } finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();


                }

                // 通知UI
                if (mCallback != null) {
                    mCallback.onAddResult(isSuccess);
                }
            }
        }
    }

    @Override
    public void delAlbum(Album album) {

        synchronized (mLock) {

            SQLiteDatabase db = null;

            boolean isSuccess = false;
            try {
                db = mXimalayaDBHelper.getWritableDatabase();
                db.beginTransaction();

                int delete = db.delete(Constants.SUB_TB_NAME, Constants.SUB_ALBUM_ID + "=?", new String[]{album.getId() + ""});

                LogUtil.d(TAG, "删除的个数 -->" + delete);

                db.setTransactionSuccessful();

                isSuccess = true;

            } catch (Exception e) {
                e.printStackTrace();
                isSuccess = false;
            } finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();

                }


                // 通知UI
                if (mCallback != null) {
                    mCallback.onDelResult(isSuccess);
                }
            }
        }
    }

    @Override
    public void listAlbums() {

        synchronized (mLock) {

            List<Album> result = new ArrayList<>();

            SQLiteDatabase db = null;
            boolean isSuccess = false;

            try {
                db = mXimalayaDBHelper.getReadableDatabase();
                db.beginTransaction();


                Cursor query = db.query(Constants.SUB_TB_NAME, null, null, null, null, null, Constants.SUB_ID + " desc");

                // 封装数据
                while (query.moveToNext()) {
                    Album album = new Album();

                    // 设置封面
                    String coverUrl = query.getString(query.getColumnIndex(Constants.SUB_COVER_URL));
                    album.setCoverUrlLarge(coverUrl);
                    album.setCoverUrlMiddle(coverUrl);
                    album.setCoverUrlSmall(coverUrl);

                    // 设置标题
                    String title = query.getString(query.getColumnIndex(Constants.SUB_TITLE));
                    album.setAlbumTitle(title);

                    // 设置描述
                    String description = query.getString(query.getColumnIndex(Constants.SUB_DESCRIPTION));
                    album.setAlbumIntro(description);

                    // 设置tracks数量
                    int tracksCount = query.getInt(query.getColumnIndex(Constants.SUB_TRACK_COUNT));
                    album.setIncludeTrackCount(tracksCount);

                    // 设置播放数量
                    int playCount = query.getInt(query.getColumnIndex(Constants.SUB_PLAY_COUNT));
                    album.setPlayCount((long) playCount);

                    // 设置作者名字
                    String authorName = query.getString(query.getColumnIndex(Constants.SUB_AUTHOR_NAME));

                    Announcer announcer = new Announcer();
                    announcer.setNickname(authorName);
                    album.setAnnouncer(announcer);

                    // 设置专辑id
                    int albumId = query.getInt(query.getColumnIndex(Constants.SUB_ALBUM_ID));
                    album.setId(albumId);

                    result.add(album);
                }

                isSuccess = true;

                query.close();
                db.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
                // LogUtil.d(TAG, "数据库查询有错吗");
                isSuccess = false;
            } finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();
                }
                // 通知UI
                if (mCallback != null) {

                    if (isSuccess) {
                        mCallback.onSubListLoaded(result);
                    }
                }
            }
        }

    }
}
